import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:yqys_target/utils/yq_screen_util.dart';
import '../../../widgets/yq_step_indicator.dart';
import '../../../gen/assets.gen.dart';
import '../controllers/organization_auth_controller.dart';
import 'widgets/basic_info_page.dart';
import 'widgets/business_info_page.dart';
import 'widgets/organization_info_page.dart';
import 'widgets/financial_info_page.dart';

class OrganizationAuthPage extends StatelessWidget {
  const OrganizationAuthPage({super.key});

  @override
  Widget build(BuildContext context) {
    final controller = Get.put(OrganizationAuthController());

    return Scaffold(
      body: Column(
        children: [
          // 头部Stack
          _HeadStepWidget(controller: controller),
          const SizedBox(
            height: 30,
          ),
          // PageView内容
          _MiddlePageView(controller: controller),
          // 底部按钮区域
          _BottomButton(controller: controller),
        ],
      ),
    );
  }
}

class _BottomButton extends StatelessWidget {
  const _BottomButton({
    required this.controller,
  });

  final OrganizationAuthController controller;

  @override
  Widget build(BuildContext context) {
    return Container(
      padding: const EdgeInsets.all(16),
      child: GetBuilder<OrganizationAuthController>(
        init: controller,
        builder: (controller) => Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            if (controller.currentPage == 3) ...[
              Row(
                children: [
                  Checkbox(
                    value: controller.agreeProtocol,
                    onChanged: controller.toggleAgreeProtocol,
                  ),
                  const Text('我已阅读并同意'),
                  TextButton(
                    onPressed: () {
                      // TODO: 显示协议内容
                    },
                    child: Text(
                      '《公益机构入驻协议》',
                      style: TextStyle(
                          color: Theme.of(context).primaryColor,
                          fontWeight: FontWeight.bold),
                    ),
                  ),
                ],
              ),
              const SizedBox(height: 5),
            ],
            Padding(
              padding:
                  EdgeInsets.only(bottom: YQScreenUtil.getBottomSafeHeight()),
              child: InkWell(
                onTap: controller.currentPage == 3
                    ? (controller.agreeProtocol
                        ? controller.handleSubmitAuth
                        : null)
                    : controller.handleNextStep,
                child: Container(
                  alignment: Alignment.center,
                  padding: const EdgeInsets.symmetric(vertical: 16),
                  decoration: BoxDecoration(
                    color: controller.currentPage == 3
                        ? Colors.grey
                        : Colors.green,
                    borderRadius: BorderRadius.circular(100),
                  ),
                  width: double.infinity,
                  child: Text(
                    controller.currentPage == 3 ? '提交认证' : '下一步',
                    style: const TextStyle(
                        fontSize: 20,
                        color: Colors.white,
                        fontWeight: FontWeight.bold),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

class _MiddlePageView extends StatelessWidget {
  const _MiddlePageView({
    required this.controller,
  });

  final OrganizationAuthController controller;

  @override
  Widget build(BuildContext context) {
    return Expanded(
      child: PageView(
        controller: controller.pageController,
        physics: const NeverScrollableScrollPhysics(),
        onPageChanged: controller.onPageChanged,
        children: [
          BasicInfoPage(),
          BusinessInfoPage(),
          OrganizationInfoPage(),
          FinancialInfoPage(),
        ],
      ),
    );
  }
}

class _HeadStepWidget extends StatelessWidget {
  const _HeadStepWidget({
    required this.controller,
  });

  final OrganizationAuthController controller;

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: 210 + YQScreenUtil.getStatusBarHeight(),
      child: Stack(
        clipBehavior: Clip.none,
        children: [
          // 背景图
          Positioned.fill(
            child: Container(
              decoration: BoxDecoration(
                borderRadius: const BorderRadius.only(
                  bottomLeft: Radius.circular(30),
                  bottomRight: Radius.circular(30),
                ),
                image: DecorationImage(
                  image: Assets.images.organizationHeaderBg.provider(),
                  fit: BoxFit.cover,
                ),
              ),
            ),
          ),
          // 返回按钮和标题
          Positioned(
            top: YQScreenUtil.getStatusBarHeight(),
            left: 16,
            child: IconButton(
              icon: const Icon(Icons.arrow_back_ios, color: Colors.white),
              onPressed: () {
                if (controller.stepController.currentStep > 0) {
                  controller.stepController.previousStep();
                  controller.pageController.previousPage(
                    duration: const Duration(milliseconds: 100),
                    curve: Curves.linear,
                  );
                } else {
                  Navigator.pop(context);
                }
              },
            ),
          ),
          Positioned(
            top: YQScreenUtil.getStatusBarHeight() + 13,
            left: 100,
            right: 100,
            child: const Text(
              '公益机构认证',
              textAlign: TextAlign.center,
              style: TextStyle(
                  color: Colors.white,
                  fontSize: 22,
                  fontWeight: FontWeight.bold),
            ),
          ),
          // 步骤指示器
          Positioned(
            left: 0,
            right: 0,
            bottom: -30,
            child: YQStepIndicator(
              steps: controller.steps,
              controller: controller.stepController,
            ),
          ),
          Positioned(
            top: YQScreenUtil.getStatusBarHeight() + 56,
            left: 22,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                const Text(
                  '感召全员参与 转递人间大爱',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                      color: Colors.white,
                      fontSize: 20,
                      fontWeight: FontWeight.bold),
                ),
                const SizedBox(height: 13),
                SizedBox(
                  height: 23,
                  child: AspectRatio(
                    aspectRatio: 190.0 / 20.0,
                    child: Stack(
                      children: [
                        Positioned.fill(
                          child: Image.asset(
                            Assets.images.headerFocusBg.path,
                            fit: BoxFit.fill,
                          ),
                        ),
                        const Positioned.fill(
                          child: Center(
                            child: Text(
                              '为您的爱心益事保驾护航',
                              style:
                                  TextStyle(color: Colors.white, fontSize: 15),
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                )
              ],
            ),
          ),
        ],
      ),
    );
  }
}
